<?php
/**
 * BooksContent Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\erm;

use app\admin\model\erm\BooksContents as BooksContentsModel;

Class BooksContents {

    /**
     * 首页列表
     * @author zx
     * @date 2019-12-19
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'name') ) {
            $map[] = ['name', 'LIKE', '%'.$get['name'].'%'];
        }
        $status = get($get, 'status');
        if ( $status === 0 || $status === '0' || $status == 1 ) {
            $map[] = ['status', '=', $status];
        }
        if ( get($get, 'start_sort') !== NULL && get($get, 'start_sort') !== '' ) {
            $map[] = ['sort', '>=', $get['start_sort']];
        }
        if ( get($get, 'end_sort') !== NULL && get($get, 'end_sort') !== '' ) {
            $map[] = ['sort', '<=', $get['start_sort']];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return BooksContentsModel::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 更改状态
     * @author zx
     * @date 2019-12-20
     * @return \think\response
     */
    public function status(BooksContentsModel $model)
    {
        $status = (int)(!$model->status);
        $model->status = $status;
        $update = $model->save();
        if ( $update ) {
            return success(['status'=>$status]);
        }
        return failed('更新失败');
    }

    /**
     * 更改书籍
     * @param int $id
     * @param array $data 数据包
     * @author zx
     * @date 2019-12-17
     * @return boolean
     */
    public function save(int $id, array $data)
    {
        return (new BooksContentsModel())
            ->allowField(['img_url','uk_audio_url','en_audio_url','name','profiles','bid','sort','status','phonogram'])
            ->isUpdate()->save($data, ['id'=>$id]);
    }

    /**
     * 新增书籍信息
     * @param array $data 数据包
     * @author zx
     * @date 2019-11-12
     * @return boolean|integer
     */
    public function create(array $data)
    {
        $booksContents = BooksContentsModel::create($data, ['img_url','uk_audio_url','en_audio_url','name','profiles','bid','sort','status','phonogram']);
        return $booksContents->isEmpty()?false:$booksContents->id;
    }

    /**
     * 判断是否唯一
     * @param array $data
     * @author zx
     * @date 2019-11-12
     * @return boolean
     */
    public function only(array $data)
    {
        $map = [];
        $id = get($data, 'id', 0);
        if ( get($data, 'name') ) {
            $map[] = ['name', '=', $data['name']];
        }
        if ( $id <= 0 ) {
            $count = BooksModel::where($map)->count();
            if ( $count > 0 ) {
                return false;
            }
        } else {
            $idResult = BooksModel::where($map)->column('id');
            foreach ( $idResult as $_id ) {
                if ( $_id != $id ) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 批量删除
     * @param mixed $data
     * @author zx
     * @date 2019-11-12
     * @return boolean
     */
    public function destory( $data )
    {
        return BooksContentsModel::destroy(function($query)use($data){
            $where = ['id', '=', $data];
            if ( is_array($data) ) {
                $where = ['id', 'in', $data];
            }
            $query->where([
                $where
            ]);
        });
    }

}